package be.dch.typeditor.parser;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.log4j.Logger;

import be.dch.typeditor.domain.Typ;

public final class TypParser {

    private TypParser() {
	// No constructor needed.
    }

    private static final Logger LOG = Logger.getLogger(TypParser.class);

    public static void toTyp(final Typ typ, final File inputfile) {
	TypParser.LOG.info("Marchalling from file: " + inputfile.getName());
	// TODO

    }

    /**
     * 
     * @param icon
     * @return
     * @throws IOException
     */
    public static void toFile(final Typ typ, final File outputfile) throws IOException {
	TypParser.LOG.info("Unmarchalling to file: " + outputfile.getName());
	String[] response = new String[] {};
	for (int i = 0; i < typ.getPois().length; i++) {
	    response = GarminIconParser.toStringArray(response, typ.getPois()[i]);
	}
	for (int i = 0; i < typ.getPolylines().length; i++) {
	    response = GarminIconParser.toStringArray(response, typ.getPolylines()[i]);
	}
	for (int i = 0; i < typ.getPolyglons().length; i++) {
	    response = GarminIconParser.toStringArray(response, typ.getPolyglons()[i]);
	}
	final FileOutputStream fos = new FileOutputStream(outputfile);
	for (final String element : response) {
	    fos.write(element.getBytes("UTF-8"));
	}
	fos.flush();
	fos.close();

    }

}
